package com.chenfan.magic.service.impl;

import com.chenfan.magic.constant.SecureConstant;
import com.chenfan.magic.entity.ClientDetails;
import com.chenfan.magic.service.IClientDetails;
import com.chenfan.magic.service.IClientDetailsService;
import lombok.AllArgsConstructor;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * 获取客户端详情
 *
 * @author troy
 */
@Service
@AllArgsConstructor
public class ClientDetailsServiceImpl implements IClientDetailsService {

    private final JdbcTemplate jdbcTemplate;

    @Override
    public IClientDetails loadClientByClientId(String clientId) {
        try {
            return jdbcTemplate.queryForObject(SecureConstant.DEFAULT_SELECT_STATEMENT, new String[]{clientId}, new BeanPropertyRowMapper<>(ClientDetails.class));
        } catch (Exception ex) {
            return null;
        }
    }

    @Override
    public Date getRealExpireTime(String clientId, Long userId) {
        try {
            Map<String, Object> isvUser = jdbcTemplate.queryForMap(SecureConstant.SELECT_ISV_USER_STATEMENT, new String[]{clientId, (userId + "")});
            return (Date) isvUser.get("expire_time");
        } catch (Exception ex) {
            return null;
        }
    }

}
